/*
 * Copyright 2009, Gregg Tavares
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Gregg Tavares nor the names of his
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// *********************** ConsoleWindow **********************************

/***
 * A ConsoleWindow is a window that shows text.
 * @constructor
 * @param {string} path URI of file to load.
 */
function __ConsoleWindow(path) {
  this.filePath = path;
}

/**
 * Makes __ConsoleWindow inherit from __DraggableWindow
 */
__ConsoleWindow.prototype = new __DraggableWindow();

/**
 * Initialized a__ConsoleWindow.
 * @param {Object} spec Initialization parameters. See __DraggableWindow.init.
 */
__ConsoleWindow.prototype.init = function(spec) {
  __DraggableWindow.prototype.init.call(this, spec);
  var pack = g_client.createPack();
  this.appPack = pack;

  this.canvasLib = o3djs.canvas.create(pack, this.root, this.viewInfo);
  this.canvasPaint = pack.createObject('CanvasPaint');
  this.viewInfo.drawContext.view = g_math.matrix4.lookAt(
      [0, 0, 1],   // eye
      [0, 0, 0],   // target
      [0, 1, 0]);  // up

  this.canvasWidth = 1024;
  this.canvasHeight = 1024;
  this.canvasQuad = this.canvasLib.createXYQuad(
      0, 0, -2, this.canvasWidth, this.canvasHeight, true);

  this.lines = [''];
  this.maxLines = 100;
  this.bottomLine = 0;
  this.topLine = 0;

  this.updateText();
  this.setProjection();
};

/**
 * Closes a __ConsoleWindow
 */
__ConsoleWindow.prototype.close = function() {
  this.appPack.destroy();
  __Window.prototype.resizeWindow.close(this, size);
};

/**
 * Sets the projection matrix
 */
__ConsoleWindow.prototype.setProjection = function() {
  this.setOrthographic(
    0 + 0.5,
    this.rect.width + 0.5,
    this.rect.height + 0.5,
    0 + 0.5,
    0.001,
    1000);
};

/**
 * Resizes this window.
 * @param {Point} size where x = width, y = height.
 */
__ConsoleWindow.prototype.resizeWindow = function(size) {
  __Window.prototype.resizeWindow.call(this, size);
  this.setProjection();
};

/**
 * Advances to next line.
 */
__ConsoleWindow.prototype.advanceToNextLine = function() {
  ++this.bottomLine;
  if (this.bottomLine >= this.maxLines) {
    this.bottomLine = 0;
  }
  this.lines[this.bottomLine] = '';
  if (this.bottomLine == this.topLine) {
    ++this.topLine;
    if (this.topLine >= this.maxLines) {
      this.topLine = 0;
    }
  }
};

/**
 * Adds text.
 */
__ConsoleWindow.prototype.addText = function(text) {
  var lines = text.split('\n');
  for (var ii = 0; ii < lines.length; ++ii) {
    var line = lines[ii];
    this.lines[this.bottomLine] += line;
    if (ii > 0) {
      this.advanceToNextLine();
    }
  }
  this.updateText();
};

/**
 * Redraws the text.
 */
__ConsoleWindow.prototype.updateText = function() {
  var canvas = this.canvasQuad.canvas;
  canvas.clear([0, 0, 0, 0]);
  canvas.saveMatrix();
  var paint = this.canvasPaint;
  paint.textAlign = o3djs.base.o3d.CanvasPaint.LEFT;
  paint.textSize = 12;
  paint.textTypeface = 'Arial';
  paint.color = [1, 1, 1, 1];
  var metrics = paint.getFontMetrics();
  var glyphTop = -metrics.ascent;
  var fontHeight = glyphTop + metrics.descent;

  var count = 1;
  var ii = this.topLine;
  while (true) {
    var y = count * fontHeight + glyphTop;
    canvas.drawText(this.lines[ii], 0, y, paint);
    if (ii == this.bottomLine) {
      break;
    }
    ++ii;
    if (ii == this.maxLines) {
      ii = 0;
    }
    ++count;
  }
  canvas.restoreMatrix();
  this.canvasQuad.updateTexture();
};

